package com.efrei.uchat.client.gui.control;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JList;
import javax.swing.JPanel;

import org.apache.log4j.Logger;

import com.efrei.uchat.client.gui.view.DefaultTab;
import com.efrei.uchat.client.model.Croom;

public class DefaultTabControl extends TabControl implements MouseListener, KeyListener, ActionListener {

	private static Logger logger = Logger.getLogger(DefaultTabControl.class);
	
	private UChatListModel<Croom> listCroomsModel;
	private DefaultTab tab;

	public DefaultTabControl(UChatControl context) {
		super(context);
		this.listCroomsModel = new UChatListModel<Croom>();
	}
	
	@Override
	public void update() {
		// automatic with DefaultListModel
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e!=null) {
			if(e.getSource()==tab.getBtnAdd()) {
				// add croom request
				getContext().newCroom();
			} else if(e.getSource()==tab.getBtnRefresh()) {
				// refresh crooms list
				logger.info("refresh crooms list");
				getContext().getCroomsList();
			}
		}
	}
	
	@Override
	public void setTab(JPanel tab) {
		this.tab = (DefaultTab) tab;
		// subscribe to list's selection and set model
		this.tab.getBtnAdd().addActionListener(this);
		this.tab.getBtnRefresh().addActionListener(this);
		JList list = this.tab.getListCrooms();
		list.setModel(listCroomsModel);
		list.addMouseListener(this);
		list.addKeyListener(this);
	}
	
	/**
	 * when a croom was choosed by user
	 * @param croom
	 */
	private void selectedCroom(Croom croom) {
		if(croom==null) {
			logger.error("Croom selected but null ...");
		} else {
			logger.info("Croom selected, switch to croom "+croom.getTopic());
			getContext().setCroomTab(croom);
			JList list = ((DefaultTab)getTab()).getListCrooms();
			// and remove selection in list
			list.removeSelectionInterval(0, list.getSelectedIndex());
		}
	}
	
	public void clearCrooms() {
		listCroomsModel.clear();
	}
	
	public void addCroom(Croom croom) {
		logger.info("add "+croom+" to listCroomsModel");
		listCroomsModel.addElement(croom);
	}
	
	public Croom getCroom(int index) {
		return (Croom) listCroomsModel.get(index);
	}
	
	public Croom getSelectedCroom() {
		int selected = ((DefaultTab)getTab()).getListCrooms().getSelectedIndex();
		if(selected==-1)
			return null;
		return (Croom) listCroomsModel.get(selected);
	}
	
	@Override
	public JPanel getTab() {
		return tab;
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		if(e.getClickCount()==2) {
			// croom selected
			Croom croom = getSelectedCroom();
			selectedCroom(croom);
		}
	}

	@Override
	public void mousePressed(MouseEvent e) {
	}

	@Override
	public void mouseReleased(MouseEvent e) {
	}

	@Override
	public void mouseEntered(MouseEvent e) {
	}

	@Override
	public void mouseExited(MouseEvent e) {
	}

	@Override
	public void keyTyped(KeyEvent e) {
	}

	@Override
	public void keyPressed(KeyEvent e) {
		if(e!=null) {
			if(e.getKeyCode()==KeyEvent.VK_ENTER) {
				Croom croom = getSelectedCroom();
				if(croom!=null) {
					// press enter with a croom
					selectedCroom(croom);
				}
			}
		}
	}

	@Override
	public void keyReleased(KeyEvent e) {
	}
}
